//----------------------------------------------------------------------------
// pwd.cpp
//
// display dialog to input user and password
//
// Copyright (C) 2011 Neil Butterworth
//----------------------------------------------------------------------------

#include <windows.h>
#include <string>
#include <iostream>
#include "command.h"
#include "cmdline.h"
#include "util.h"
#include "resource.h"

using namespace std;

namespace Swine {

//----------------------------------------------------------------------------
// Command & option names
//----------------------------------------------------------------------------

const char * const PWD_CMD 			= "userpass";
const char * const CAPTION_OPT 		= "caption";
const char * const USER_OPT 		= "user";
const char * const PWD_OPT 			= "pwd";
const char * const SEP_OPT 			= "sep";

const char * const SEP_CHAR			= ",";
const char * const CAPTION_STR		= "Enter user and password";

//----------------------------------------------------------------------------
// Help
//----------------------------------------------------------------------------

const char * const PWD_HELP =
	 "display user name and password input dialog\n"
	"options:\n"
	" --caption     specify message box caption (default is 'Enter user and password') \n"
	" --user        initial value for user input (default is empty string)\n"
	" --pwd         initial value for password input (default is empty string)\n"
    " --sep         separator for output - default is ','\n";

//----------------------------------------------------------------------------
// Command class and registration
//----------------------------------------------------------------------------

class PwdCmd : public Command {

	public:

		PwdCmd() : Command( PWD_CMD, PWD_HELP ) {}

		BOOL ProcessControlMessages( HWND wnd, int ctrl, int msg ) const;
		int Execute( CmdLine & cl ) const;
		void InitDialog( HWND dlg, CmdLine & cl  ) const;

	private:

};

static AddCommand <PwdCmd> cmd1_;

//----------------------------------------------------------------------------
// Handle WM_COMMAND messages - we are only interested in button clicks
//----------------------------------------------------------------------------

static string Separator = "|";

BOOL PwdCmd :: ProcessControlMessages( HWND hwnd, int ctrl, int msg ) const {

	if ( ctrl == IDCANCEL && msg == BN_CLICKED ) {
		Reply( "" );
		DestroyWindow( hwnd );
		return TRUE;
	}
	else if ( ctrl == IDOK && msg == BN_CLICKED ) {
		Reply( TextFromCtrl( hwnd, IDC_USEREDIT ) + Separator +
					TextFromCtrl( hwnd, IDC_PWDEDIT ) );
		DestroyWindow( hwnd );
		return TRUE;
	}
	else {
		return TRUE;
	}
}

//----------------------------------------------------------------------------

int PwdCmd :: Execute( CmdLine & cl ) const {
	DoDialog( IDD_PWDDLG, cl );
	return 0;
}

//----------------------------------------------------------------------------

void PwdCmd :: InitDialog( HWND dlg, CmdLine & cl ) const {
	string caption = cl.ExtractOption( CAPTION_OPT, CAPTION_STR );
	string user = cl.ExtractOption( USER_OPT, "" );
	string pwd = cl.ExtractOption( PWD_OPT, "" );
	Separator = cl.ExtractOption( SEP_OPT, "," );
	SetWindowText( dlg, caption.c_str() );
	SetWindowText( MustGetControl( dlg, IDC_USEREDIT  ), user.c_str() );
	SetWindowText( MustGetControl( dlg, IDC_PWDEDIT  ), pwd.c_str() );
	SetFocus( dlg, IDC_USEREDIT );
	SendMessage( MustGetControl( dlg, IDC_USEREDIT  ), EM_SETSEL, 0, -1 );
	cl.MustBeEmpty();
}

} // namespace
